package com.ruoyi.web.controller.api.shop;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.system.domain.PStoreOrder;
import com.ruoyi.system.domain.bo.*;
import com.ruoyi.system.domain.vo.PreOrderVo;
import com.ruoyi.system.service.IPStoreOrderService;
import com.ruoyi.system.service.IPStoreProductReplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2022-08-01
 */
@Api(value = "订单控制器", tags = {"前端-商城订单管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/store/order")
public class PStoreOrderApiController extends BaseController {

    private final IPStoreOrderService iPStoreOrderService;
    private final IPStoreProductReplyService replyService;


    @ApiOperation("预下单")
    @RepeatSubmit()
    @PostMapping("/pre-order")
    public R<Map<String, Object>> preOrder(@RequestBody @Validated PreOrderVo request) {
        return R.ok(iPStoreOrderService.preOrder(request));
    }

    /**
     * 加载预下单
     */
    @ApiOperation(value = "加载预下单")
    @GetMapping("load/pre/{preOrderNo}")
    public R<PreOrderResponse> loadPreOrder(@PathVariable String preOrderNo) {
        return R.ok(iPStoreOrderService.loadPreOrder(preOrderNo));
    }

    /**
     * 创建订单
     */
    @ApiOperation(value = "创建订单")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public R<Map<String, Object>> createOrder(@Validated @RequestBody CreateOrderRequest orderRequest) {
        return R.ok(iPStoreOrderService.createOrder(orderRequest));
    }

    /**
     * 查询订单列表
     */
    @ApiOperation("查询订单列表")
    @GetMapping("/list")
    public TableDataInfo<OrderDetailResponse> list(PStoreOrderBo bo, PageQuery pageQuery) {
        return iPStoreOrderService.queryPageMyFontList(bo, pageQuery);
    }

    @ApiOperation("订单详细信息")
    @GetMapping("/detail/{id}")
    public R<StoreOrderDetailInfoResponse> orderDetail(@ApiParam("主键")
                                    @NotNull(message = "主键不能为空")
                                    @PathVariable("id") Long id) {
        return R.ok(iPStoreOrderService.detailOrder(id));
    }

    /**
     * 新增订单
     */
    @ApiOperation("新增订单")
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PStoreOrderBo bo) {
        return toAjax(iPStoreOrderService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改订单
     */
    @ApiOperation("修改订单")
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PStoreOrderBo bo) {
        return toAjax(iPStoreOrderService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除订单
     */
    @ApiOperation("删除订单")
    @GetMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iPStoreOrderService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @ApiOperation("评价订单")
    @RepeatSubmit()
    @PostMapping("/comment")
    public R<Void> comment(@Validated(AddGroup.class) @RequestBody PStoreProductReplyBo bo) {
        replyService.insertByBo(bo);
        return R.ok();
    }

    @ApiOperation("订单收货")
    @PostMapping("/take-order")
    public R<Void> takeOrder(@RequestBody PStoreOrderBo bo) {
        iPStoreOrderService.takeOrder(bo);
        return R.ok();
    }

    @ApiOperation("取消订单")
    @PostMapping("/cancel-order")
    public R<Void> cancelOrder(@RequestBody PStoreOrderBo bo) {
        iPStoreOrderService.cancelOrder(bo);
        return R.ok();
    }

    @ApiOperation("积分兑换记录列表")
    @GetMapping("/integralRecordList")
    public TableDataInfo<PStoreOrder> integralRecordList(PageQuery pageQuery) {
        return this.iPStoreOrderService.integralRecordList(pageQuery);
    }

    @ApiOperation("商城订单详情")
    @GetMapping("/shopOrderDetail")
    public R<List<PStoreOrder>> shopOrderDetail(@RequestParam("orderId") String orderId) {
        return R.ok(this.iPStoreOrderService.shopOrderDetail(orderId));
    }

    @ApiOperation("商城个人中心")
    @GetMapping("/shopPersonCenter")
    public R<Map<String, Object>> shopPersonCenter() {
        return R.ok(this.iPStoreOrderService.shopPersonCenter());
    }

    @ApiOperation("商城我的订单列表")
    @GetMapping("/shopOrderList")
    public R<List<PStoreOrder>> shopOrderList(@RequestParam("ind") String ind, PageQuery pageQuery) {
        return R.ok(this.iPStoreOrderService.shopOrderList(ind, pageQuery));
    }

    @ApiOperation("申请退款")
    @PostMapping("/refundApply")
    public R<Void> refundApply(@RequestBody PStoreOrder storeOrder) {
        this.iPStoreOrderService.refundApply(storeOrder);
        return R.ok();
    }
}
